﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using Gift.Model;
using Gift.DAL;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class goodsCompanyEdit01 : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
        readonly dalGoodsCompany theGoodsCompany = new dalGoodsCompany();
        readonly DALBrand theBrand = new DALBrand();
        readonly DALTerms theTerms = new DALTerms();
        readonly DALModelType theModelType = new DALModelType();
        readonly DALDeliveryTemplate theDeliveryTemplate = new DALDeliveryTemplate();
        tbGoodsCompany oGoodsCompany;

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["gid"] == null)
                {
              
                    if (Session["goodsCode"] == null)
                    {
                        Session["goodsCode"] = DateTime.Now.Ticks.ToString();
                    }
                  
                }
                this.Display("0", "|--");
                this.BindBrand();
                this.BindForm();
               
            }
        }




        void BindForm()
        {
            if (Request.QueryString["gid"] != null)
            {
                oGoodsCompany = theGoodsCompany.GetModel(int.Parse(Request.QueryString["gid"]));
                this.txtGoodsName.Text = oGoodsCompany.goodsName;
                this.txtGoodsBatchNo.Text = oGoodsCompany.goodsCode;
                if (this.ddlGoodsType.Items.FindByValue(oGoodsCompany.typeId.ToString()) != null)
                {
                    this.ddlGoodsType.SelectedValue = this.ddlGoodsType.Items.FindByValue(oGoodsCompany.typeId.ToString()).Value;
                }
                if (this.ddlGoodsBrand.Items.FindByValue(oGoodsCompany.brandId.ToString()) != null)
                {
                    this.ddlGoodsBrand.SelectedValue = this.ddlGoodsBrand.Items.FindByValue(oGoodsCompany.brandId.ToString()).Value;
                }
           
                this.lblBigPic.Text = oGoodsCompany.smallPic.ToString();
              
                this.fileGoodsPhoto.Required = false;
                this.fileGoodsPhoto.ShowRedStar = false;

          
                this.txtGoodsPrice.Text = oGoodsCompany.goodsPrice.ToString();
        

            }

        }

         void UploadBig()
        {
              if (this.fileGoodsPhoto.FileName.Length / 1024 > 300)
            {
                ExtAspNet.Alert.Show("图片大小不能超过300kb！");
            }
              
              else  if (this.fileGoodsPhoto.FileName.Length > 0)
            {

                string fileName = this.fileGoodsPhoto.ShortFileName;
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                if (this.fileGoodsPhoto.HasFile)
                {
                    this.fileGoodsPhoto.SaveAs(Server.MapPath("~/uploadfile/product/" + fileName));
                }
                this.lblBigPic.Text = fileName;

            }
           
            else
            {
                ExtAspNet.Alert.Show("请选择上传的图片！");
            }
        }

        protected void btnSave01_Click(object sender, EventArgs e)
        {
    
            if (Request.QueryString["gid"] != null)
            {
               
                oGoodsCompany = theGoodsCompany.GetModel(int.Parse(Request.QueryString["gid"]));
                oGoodsCompany.goodsName = this.txtGoodsName.Text.Trim();
                oGoodsCompany.goodsCode = this.txtGoodsBatchNo.Text.Trim();
                oGoodsCompany.typeId = int.Parse(this.ddlGoodsType.SelectedItem.Value);
                if (this.ddlGoodsBrand.SelectedItem.Value != "-1")
                {
                    oGoodsCompany.brandId = int.Parse(this.ddlGoodsBrand.SelectedItem.Value);
                }
         
         
         
                if (this.fileGoodsPhoto.FileName.Length > 0)
                {
                    this.UploadBig();
                }
         
                
                oGoodsCompany.smallPic = this.lblBigPic.Text;
           
                oGoodsCompany.goodsPrice = decimal.Parse(this.txtGoodsPrice.Text);
        
                theGoodsCompany.Update(oGoodsCompany);
                ExtAspNet.Alert.Show("保存成功！", "", "window.parent.location='goodsCompany.aspx?gid=" + oGoodsCompany.goodsId.ToString() + "'");

            }
            else
            {
                if (this.fileGoodsPhoto.FileName.Length <=0)
                {
                    ExtAspNet.Alert.Show("请选择上传的大图！");
                    return;
                }
              
                this.UploadBig();
               // this.UploadSmall();
                oGoodsCompany = new tbGoodsCompany();
                oGoodsCompany.goodsName = this.txtGoodsName.Text.Trim();
                oGoodsCompany.goodsCode = this.txtGoodsBatchNo.Text.Trim();
                oGoodsCompany.typeId = int.Parse(this.ddlGoodsType.SelectedItem.Value);
                if (this.ddlGoodsBrand.SelectedItem.Value != "-1")
                {
                    oGoodsCompany.brandId = int.Parse(this.ddlGoodsBrand.SelectedItem.Value);
                }        
                oGoodsCompany.smallPic = this.lblBigPic.Text;
                oGoodsCompany.addTime = DateTime.Now;
                oGoodsCompany.companyId = int.Parse(base.CompanyId);
                oGoodsCompany.goodsPrice = decimal.Parse(this.txtGoodsPrice.Text);
              
               int pid = theGoodsCompany.Add(oGoodsCompany);

               if (Session["goodsCode"] != null )
               {
                   Session["goodsCode"] = null;
               }
               ExtAspNet.Alert.Show("保存成功！", "", "window.parent.location='goodsCompany.aspx?gid=" + pid.ToString() + "'");
       

            }
        }




        private void Display(string parentid, string space)
        {
            string sql = "Select * From tbGoodsType Where parentId =" + parentid + " and isnull(visible,0)=1 Order By typeOrder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    ExtAspNet.ListItem lt = new ExtAspNet.ListItem(space + "" + row["typeName"].ToString() + "", "" + row["typeId"].ToString() + "");
                    if (row["typedepth"] == null || row["typedepth"].ToString() != "2")
                    {
                        lt.EnableSelect = false;
                    }
                    this.ddlGoodsType.Items.Add(lt);
                    this.Display(row["typeId"].ToString(), "|-" + space);
                }
            }
            if (ddlGoodsType.Items.Count <=0 || ddlGoodsType.Items[0].Value != "-1")
            {
                this.ddlGoodsType.Items.Insert(0, new ExtAspNet.ListItem("请选择...", "-1"));
                this.ddlGoodsType.SelectedValue = this.ddlGoodsType.Items[0].Value;
            }
        }

        void BindBrand()
        {
            DataTable dt = theBrand.GetList("visible = 1").Tables[0];
            this.ddlGoodsBrand.DataTextField = "brandName";
            this.ddlGoodsBrand.DataValueField = "brandId";
            this.ddlGoodsBrand.DataSource = dt;
            this.ddlGoodsBrand.DataBind();
             this.ddlGoodsBrand.Items.Insert(0,new ExtAspNet.ListItem("请选择...","-1"));
            this.ddlGoodsBrand.SelectedValue = this.ddlGoodsBrand.Items[0].Value;
        }



    }
}